package com.taotao.test2.easy1;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @program: test2
 * @ClassName Tes6t
 * @description:
 * @author: wangjin
 * @create: 2023-06-09 15:45
 * @Version 1.0
 **/
@RestController
public class Tes6t {
  /**
     * 导出
     *
     * @param response 响应
     * @throws IOException ioexception
     */
  @RequestMapping("/export")
  public void export(HttpServletResponse response) throws IOException {
      String fileName = System.currentTimeMillis() + "";
      fileName = URLEncoder.encode(fileName, "UTF-8");
      response.setContentType("application/vnd.ms-excel");
      response.setCharacterEncoding("UTF-8");
      response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
      // 提前构建输出流
      ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
      // 构建sheet页--表示不加表头
      WriteSheet writeSheet = EasyExcel.writerSheet("sheet").needHead(Boolean.FALSE).build();
      // 表头的数量
      int num = 0;
      // 模拟写5张表
      for (int i = 0; i < 1; i++) {
          // 获取第二行表头
          List<List<String>> contentHeader = this.buildHeader();
          // 生成第一行表头--这样两行表头长宽一致
          List<List<String>> nameHeader = contentHeader.stream().map(item -> Collections.singletonList("城市表")).collect(Collectors.toList());
          // 这里必须指定需要头，table 会继承sheet的配置，sheet配置了不需要，table 默认也是不需要
          // 第一次写入会创建头
          WriteTable writeTable0 = EasyExcel.writerTable(num).needHead(Boolean.TRUE).head(nameHeader).build();
          excelWriter.write((Collection<?>) null, writeSheet, writeTable0);

          WriteTable writeTable1 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(contentHeader).build();
          List<List<String>> body = new ArrayList<>();
          excelWriter.write(body, writeSheet, writeTable1);
          // 插入两次表头加2
          num = num + 2;
      }
      excelWriter.finish();
  }


    /**
     * 模拟表头
     *
     * @return {@link List}<{@link List}<{@link String}>>
     */
    public List<List<String>> buildHeader() {
        List<List<String>> header = new ArrayList<>();
        header.add(Collections.singletonList("序号"));
        header.add(Collections.singletonList("城市"));
        header.add(Collections.singletonList("省会"));
        header.add(Collections.singletonList("得分"));
        return header;
    }

    /**
     * 模拟表内容
     *
     * @return {@link List}<{@link List}<{@link String}>>
     */


}
